package com.example.mykotlin.base.util

import com.example.mykotlin.base.config.cache.CacheConfig
import com.example.mykotlin.service.CommonService
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import org.redisson.api.RedissonClient
import org.springframework.cache.caffeine.CaffeineCacheManager
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Component

@Component
class GetBean : ApplicationContextAware {
    companion object {
        private var objectMapper: ObjectMapper? = null
        private var jdbcTemplate: JdbcTemplate? = null
        private var redisTemplate: RedisTemplate<String, Any>? = null
        private var redissonClient: RedissonClient? = null
        private var commonService: CommonService? = null
        private var caffeineCacheManager: CaffeineCacheManager? = null

        fun objectMapper() = objectMapper!!
        fun jdbcTemplate() = jdbcTemplate!!
        fun redisTemplate() = redisTemplate!!
        fun redissonClient() = redissonClient!!
        fun commonService() = commonService!!
        fun caffeineCacheManager() = caffeineCacheManager!!
    }

    override fun setApplicationContext(applicationContext: ApplicationContext) {
        objectMapper = applicationContext.getBean(ObjectMapper::class.java)
        jdbcTemplate = applicationContext.getBean(JdbcTemplate::class.java)
        redisTemplate =
            applicationContext.getBean("redisTemplate", RedisTemplate::class.java) as RedisTemplate<String, Any>
        redissonClient = applicationContext.getBean(RedissonClient::class.java)
        commonService = applicationContext.getBean(CommonService::class.java)
        caffeineCacheManager = applicationContext.getBean(CacheConfig.LOCAL_CACHE, CaffeineCacheManager::class.java)
    }


}